package com.dao;

import com.utils.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RecordDao {
    /**
     * 记录所有的用户请求信息 (访问日志)
     * @param url
     * @param ip
     * @param userAgent
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void insert(String url, String ip, String userAgent) throws SQLException, ClassNotFoundException {
        String sql = "insert into record (record_url, record_ip, record_ua) values (?, ?, ?)";
        System.out.println("要执行的语句是：" + sql);

        // 创建连接对象
        try (Connection connection = DBHelper.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)){
            statement.setString(1, url);
            statement.setString(2, ip);
            statement.setString(3, userAgent);
            statement.executeUpdate();
        }
    }

    // 测试方法
    public static void main(String[] args) throws Exception {
        RecordDao recordDao = new RecordDao();
        recordDao.insert("/book/list", "1.2.1.1", "IE");
        System.out.println("插入成功!");
    }
}
